﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using IMCOM.Imagiality.Core;

namespace IMCOM.Imagiality.MainGUI
{
    public class ContrastBalanceGUI : Panel
    {
        private GroupBox groupBoxContrastBalance;
        private Button buttonFindPoints;
        private Button buttonAutoContrast;
        private GroupBox groupBoxManualBalance;
        private Panel panelWhiteBalance;
        private Panel panelBlackBalance;
        private Label labelWhite;
        private Label labelBlack;
        private NumericUpDown numericUpDownWhite;
        private NumericUpDown numericUpDownBlack;
        private IList<ContrastBalanceGUIListener> listeners;

        public ContrastBalanceGUI()
        {
            listeners = new List<ContrastBalanceGUIListener>();

            Width = 150;
            Height = 150;

            groupBoxContrastBalance = new GroupBox();
            groupBoxContrastBalance.Text = "Contrast Balance";

            Controls.Add(groupBoxContrastBalance);

            buttonFindPoints = new Button();
            buttonFindPoints.Text = "Find Points";
            groupBoxContrastBalance.Controls.Add(buttonFindPoints);

            buttonAutoContrast = new Button();
            buttonAutoContrast.Text = "Auto Contrast";
            groupBoxContrastBalance.Controls.Add(buttonAutoContrast);

            groupBoxManualBalance = new GroupBox();
            groupBoxManualBalance.Text = "Manual Balance";

            groupBoxContrastBalance.Controls.Add(groupBoxManualBalance);

            panelWhiteBalance = new Panel();
            groupBoxManualBalance.Controls.Add(panelWhiteBalance);

            labelWhite = new Label();
            panelWhiteBalance.Controls.Add(labelWhite);
            labelWhite.Text = "White";
            labelWhite.TextAlign = ContentAlignment.MiddleLeft;

            numericUpDownWhite = new NumericUpDown();
            panelWhiteBalance.Controls.Add(numericUpDownWhite);

            labelWhite.Dock = DockStyle.Left;
            numericUpDownWhite.Dock = DockStyle.Right;

            panelBlackBalance = new Panel();
            groupBoxManualBalance.Controls.Add(panelBlackBalance);

            labelBlack = new Label();
            panelBlackBalance.Controls.Add(labelBlack);
            labelBlack.Text = "Black";
            labelBlack.TextAlign = ContentAlignment.MiddleLeft;

            numericUpDownBlack = new NumericUpDown();
            panelBlackBalance.Controls.Add(numericUpDownBlack);

            labelBlack.Dock = DockStyle.Left;
            numericUpDownBlack.Dock = DockStyle.Right;

            Resize += new EventHandler(GUIResized);

            ArrangeLayout();
        }

        public void AddListener(ContrastBalanceGUIListener listener)
        {
            listeners.Add(listener);
        }

        private void GUIResized(object sender, System.EventArgs e)
        {
            ArrangeLayout();
        }

        private void ArrangeLayout()
        {
            groupBoxContrastBalance.Width = Width;
            groupBoxContrastBalance.Height = Height;

            buttonFindPoints.Location = new Point(5, 20);
            buttonFindPoints.Width = groupBoxContrastBalance.Width - 10;

            buttonAutoContrast.Location = new Point(5, 20 + buttonFindPoints.Height + 5);
            buttonAutoContrast.Width = groupBoxContrastBalance.Width - 10;

            groupBoxManualBalance.Top = buttonAutoContrast.Bottom + 5;
            groupBoxManualBalance.Left = 5;
            groupBoxManualBalance.Width = Width - 10;

            panelWhiteBalance.Location = new Point(5, 20);
            panelWhiteBalance.Width = Width - 20;
            panelWhiteBalance.Height = numericUpDownWhite.Height;

            labelWhite.Width = panelWhiteBalance.Width / 2;
            numericUpDownWhite.Width = panelWhiteBalance.Width / 2;

            panelBlackBalance.Location = new Point(5, 20 + numericUpDownWhite.Height + 5);
            panelBlackBalance.Width = Width - 20;
            panelBlackBalance.Height = numericUpDownBlack.Height;

            labelBlack.Width = panelBlackBalance.Width / 2;
            numericUpDownBlack.Width = panelBlackBalance.Width / 2;

            groupBoxManualBalance.Height = 20 + panelWhiteBalance.Height + 5 + panelBlackBalance.Height + 5;
        }
    }
}
